import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-09-12
 * Time: 22:35
 */


//阻塞队列

public class MyBlockingQueue {


    private Object locker = new Object();
    private int head = 0;
    private int tail = 0;
    private int size = 0;//
    private int capacity = 0;
    private int[] data = null;

    public MyBlockingQueue(int capacity) {
        this.capacity = capacity;
        data = new int[capacity];
    }


    //插入
    public void put(int val) throws InterruptedException {
        synchronized (locker) {
            //如果满了,阻塞
            while (size >= data.length) {
                locker.wait();
            }
            data[size] = val;
            size++;
            tail++;
            if (tail >= data.length) {
                tail = 0;
            }
            locker.notify();//唤醒
        }

    }

    //删除
    public void get() throws InterruptedException {
        synchronized (locker) {
            while (size == 0) {
                //
                locker.wait();
            }

            head++;
            if (head >= data.length) {
                head = 0;
            }
            size--;

            locker.notify();

        }
    }

}
